package org.example.user.contorller;

import org.example.user.domain.SysUser;
import org.example.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: UserContorller
 * @author: e
 * @createDate: 2021/5/30
 */
@RequestMapping("/user")
@RestController
public class UserContorller {

    @Autowired
    private IUserService userService;

    /***
     * 注册 true成功，false失败
     **/
    @GetMapping("/register/{email}/{password}/{code}")
    public int register(@PathVariable("email") String email,
                            @PathVariable("password") String password,
                            @PathVariable("code") String code) {
        return userService.register(email,password,code);
    }

    /***
     * 判断邮箱是否被注册过；true代表已经注册过， false代表尚未注册
     **/
    @GetMapping("/isRegister/{email}")
    public boolean isRegister(@PathVariable("email") String email) {
        return userService.findEmailIsRegister(email) == 1;
    }

    /***
     * 根据token查询⽤户登录邮箱接⼝
     **/
    @GetMapping("/info/{token}")
    public String info(@PathVariable("token") String token) {
        return userService.findEmailByToken(token);
    }


    /***
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和
     * 密码⽣成token，token存⼊数据库，并写⼊cookie
     * 中
     **/
    @GetMapping("/login/{email}/{password}")
    @ResponseBody
    public String login(@PathVariable("email") String email,
                         @PathVariable("password") String password,
                         HttpServletRequest request, HttpServletResponse response) {
        return userService.login(email,password,request,response);
    }
}
